$( function() {
  
  // check is or not select all
  $('#check_all').live('click', function() {
    $('.check').attr('checked', $(this).attr('checked'));
  });
  
  // update ojbect status(shelf on/off, delete)
  $('.update_status').live('click', function() {
    if ( $(this).attr('rel') == -1 && !confirm('确定要删除吗？') ) {
      return false;
    }
    
    if ( $('.check:checked').size() < 1 ) {
      js_flash_error('请选择要操作的选项');
      return false;
    }
    
    $('#status').val($(this).attr('rel'));
    $(this).closest('form').submit();
  });
  
  // update ojbect flag(remcommend, hot, tag, comment)
  $('.update_flag').live('click', function() {
    if ( $('.check:checked').size() < 1 ) {
      js_flash_error('请选择要操作的选项');
      return false;
    }
    
    $('#flag').val($(this).attr('rel'));
    $(this).closest('form').attr('action', '/items/update_flag').submit();
  });
  
  /******** dictionary page ******/
  // add or remove dictionary on new page
  $('#add_dictionary').live('click', function() {
    $('#add_more').before("<div class='dictionary'><input name='words[]'>&nbsp;<input type='button' value='删除' class='remove_dictionary'></div>");
  });
  
  $('.remove_dictionary').live('click', function() {
    $(this).parent().remove();
  });
  
  /******** item page ******/
  // change cat1 and cat2
  $('#item_cat1_id, #cat1_id').change( function() {
    var cat2_opts = "<option value=''>二级分类</option>";
    if ( $(this).val() ) {
      $('#item_cat1_name').val($(this).find('option[selected]').text());
      $('#item_cat2_name').val('');
      $.ajax({
        url: "/item_categories/children/" + $(this).val(),
        dataType: 'json',
        success: function(data) {
          if ( data ) {
            $(data).each( function() {
              cat2_opts += "<option value='"+this['item_category']['id']+"'>"+this['item_category']['name']+"</option>";
            });
          }
          $('#item_cat2_id, #cat2_id').html(cat2_opts);
        },
        error: function() {
          js_flash_error('载入二级分类失败!');
        }
      });
    } else {
      $('#item_cat1_name, #item_cat2_name').val('');
      $('#item_cat2_id, #cat2_id').html(cat2_opts);
    }
  });
  
  $('#item_cat2_id').change( function() {
    if ( $(this).val() ) {
      $('#item_cat2_name').val($(this).find('option[selected]').text());
    } else {
      $('#item_cat2_name').val('');
    }
  });
  
  // edit item title
  $('.item_title').live('click', function() {
    this_obj = $(this);
    $.ajax({
      url: '/items/edit_title/' + this_obj.attr('rel'),
      success: function(data) {
        this_obj.hide().after(data);
      }
    });
  });
  
  $('.submit_button').live('click', function() {
    var item_name = $(this).prev().val();
    $.ajax({
      url: '/items/edit_title/',
      type: 'post',
      data: { 'id': $(this).attr('rel'), 'name': item_name },
      success: function(data) {
        $('#edit_item_title').prev('.item_title').text(item_name).show();
        $('#edit_item_title').remove();
      },
      error: function() {
        js_flash_error('保存失败');
      }
    });
  });
  
  $('.cancel_button').live('click', function() {
    $('#edit_item_title').prev('.item_title').show();
    $('#edit_item_title').remove();
  });

  // recommend item
  $(".item_recommend").live('click', function() {
    $('#item_recommend_recommendable_id').val($(this).attr('id'));
    $('#item_recommend_name').val($(this).attr('name'));
  });
  
  // 打开/关闭弹出层
  $(".open_window").live('click', function() {
    var open_window = "."+ $(this).attr('rel');
	  $(open_window + ", .windowfade").show();
	  return false;
  });
  
  $(".close_window").live('click', function() {
	  $("."+ $(this).attr('rel') + ", .windowfade").hide();
	  return false;
  });
  
  $('#item_recommend_submit').live('click', function() {
    var submit_form = $(this).closest('form');
    $.ajax({
      url: submit_form.attr('action'),
      type: 'POST',
      data: submit_form.serialize(),
      success: function() {
        $("#window, .windowfade").hide();
        js_flash_notice('操作成功');
      },
      error: function(data) {
        js_flash_error(data.responseText);
      }
    });
    return false;
  });
  
  $('#recommend_subject').change( function() {
    $.ajax({
      url: '/recommend_subjects/get_subjects',
      data: { 'flag': $(this).val() },
      dataType: 'json',
      success: function(data) {
        var recommend_opts = '';
        if ( data ) {
          $(data).each( function() {
            recommend_opts += "<option value='"+this['recommend_subject']['id']+"'>"+this['recommend_subject']['title']+"</option>";
          });
        }
        $('#item_recommend_recommend_subject_id').html(recommend_opts);
      },
      error: function() {
        js_flash_error('操作失败');
      }
    });
  });
  
  
  // cms page
  // add recommends
  $('#add_commends').live('click', function() {
    if ( $('.check:checked').size() < 1 ) {
      js_flash_error('请选择要操作的选项');
      return false;
    }
    
    $(this).closest('form').submit();
  });
  
  $('.delete').live('click', function() {
    this_obj = $(this);
    $.ajax({
      url: '/recommend_subjects/delete_recommend',
      type: 'post',
      data: { 'id': $(this).attr('rel') },
      success: function() {
        js_flash_notice('操作成功');
        this_obj.closest('tr').remove();
      },
      error: function() {
        js_flash_error('操作失败');
      }
    });
  });
  
});
